package com.clip.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clip.dao.UserDAO;
import com.clip.dao.UserDevicetokenDAO;
import com.clip.entity.User;
import com.clip.entity.UserDevicetoken;

@Service
@Transactional
public class UserDevicetokenSerivce {
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private UserDevicetokenDAO devicetokenDAO;
	
	
	public String updateUserDevicetoken(int uid, String devicetoken) {
		User user=this.userDAO.findById(uid);
		if(user!=null&&user.getStatus()==1){
			List list=this.devicetokenDAO.findByQuery("from UserDevicetoken token where token.uid="+uid);
			if(list!=null&&list.size()>0){
				UserDevicetoken userDevicetoken=(UserDevicetoken) list.get(0);
				userDevicetoken.setDevicetoken(devicetoken);
				this.devicetokenDAO.attachDirty(userDevicetoken);
			}else{
				UserDevicetoken userDevicetoken=new UserDevicetoken();
				userDevicetoken.setUid(uid);
				userDevicetoken.setDevicetoken(devicetoken);
				this.devicetokenDAO.save(userDevicetoken);
			}
			return null;
		}else
			return "用户不存在";
	}

}
